package com.example.choupaiapp.main.util;

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.choupaiapp.R;
import com.example.choupaiapp.common.util.ToastUtil;

import java.util.HashMap;


public class PublishVoteDialog extends DialogFragment implements View.OnClickListener {

    private InputMethodManager inputMethodManager;
    private DialogPublishVoteCallback dataCallback;
    private TextView complete;
    private TextView add_vote;
    private HashMap<Integer, String> stringHashMap = new HashMap<>();
    private LinearLayout vote_tab_ll;

    @Override
    public void onAttach(Context context) {
        if (!(getActivity() instanceof DialogPublishVoteCallback)) {
            throw new IllegalStateException("DialogFragment 所在的 activity 必须实现 DialogPublishVoteCallback 接口");
        }
        super.onAttach(context);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog mDialog = new Dialog(getActivity(), R.style.BottomDialog);
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.setContentView(R.layout.dialog_publish_vote);
        mDialog.setCanceledOnTouchOutside(true);

        Window window = mDialog.getWindow();
        WindowManager.LayoutParams layoutParams;
        if (window != null) {
            layoutParams = window.getAttributes();
            layoutParams.gravity = Gravity.BOTTOM;
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes(layoutParams);
        }

        complete = mDialog.findViewById(R.id.complete);
        add_vote = mDialog.findViewById(R.id.add_vote);
        vote_tab_ll = mDialog.findViewById(R.id.vote_tab_ll);

        stringHashMap.clear();

        fillEditText();

        complete.setOnClickListener(this);
        add_vote.setOnClickListener(this);

        return mDialog;
    }


    private void fillEditText() {
        dataCallback = (DialogPublishVoteCallback) getActivity();
        if (dataCallback.getVoteList() != null && dataCallback.getVoteList().size() != 0) {
            for (int i = 0; i < dataCallback.getVoteList().size(); i++) {
                View view = View.inflate(getActivity(), R.layout.item_publish_vote, null);
                TextView vote_tab_tv = view.findViewById(R.id.vote_tab_tv);
                vote_tab_tv.setText("选项" + (i + 1));
                EditText vote_tab_et = view.findViewById(R.id.vote_tab_et);
                vote_tab_et.setText(dataCallback.getVoteList().get(i));
                ImageView vote_tab_iv = view.findViewById(R.id.vote_tab_iv);
                if (i > 1) {
                    vote_tab_iv.setVisibility(View.VISIBLE);
                } else {
                    vote_tab_iv.setVisibility(View.GONE);
                }
                vote_tab_iv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View clickview) {
                        vote_tab_ll.removeView(view);
                        for (int i = 0; i < vote_tab_ll.getChildCount(); i++) {
                            TextView vote_tab_tv = vote_tab_ll.getChildAt(i).findViewById(R.id.vote_tab_tv);
                            vote_tab_tv.setText("选项" + (i + 1));
                        }
                        add_vote.setVisibility(View.VISIBLE);
                    }
                });
                vote_tab_ll.addView(view);
            }
        } else {
            for (int i = 0; i < 2; i++) {
                View view = View.inflate(getActivity(), R.layout.item_publish_vote, null);
                TextView vote_tab_tv = view.findViewById(R.id.vote_tab_tv);
                vote_tab_tv.setText("选项" + (i + 1));
                EditText vote_tab_et = view.findViewById(R.id.vote_tab_et);
                vote_tab_et.setText("");
                ImageView vote_tab_iv = view.findViewById(R.id.vote_tab_iv);
                vote_tab_iv.setVisibility(View.GONE);
                vote_tab_ll.addView(view);
            }
        }

        EditText vote_tab_et = vote_tab_ll.getChildAt(0).findViewById(R.id.vote_tab_et);
        vote_tab_et.setFocusable(true);
        vote_tab_et.setFocusableInTouchMode(true);
        vote_tab_et.requestFocus();
        if (!TextUtils.isEmpty(dataCallback.getVoteList().get(0))) {
            vote_tab_et.setSelection(dataCallback.getVoteList().get(0).length());
        }
        vote_tab_et.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (inputMethodManager != null) {
                    if (inputMethodManager.showSoftInput(vote_tab_et, 0)) {
                        vote_tab_et.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                }
            }
        });


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.complete:
                for (int i = 0; i < vote_tab_ll.getChildCount(); i++) {
                    EditText vote_tab_et = vote_tab_ll.getChildAt(i).findViewById(R.id.vote_tab_et);
                    if (!TextUtils.isEmpty(vote_tab_et.getText().toString())) {
                        stringHashMap.put(i, vote_tab_et.getText().toString());
                    } else {
                        ToastUtil.showImageToas("选项不能为空哦");
                        return;
                    }
                }
                dataCallback.setVoteList(stringHashMap);
                dismiss();
                break;
            case R.id.add_vote:
                if (vote_tab_ll.getChildCount() < 4) {
                    View view = View.inflate(getActivity(), R.layout.item_publish_vote, null);
                    TextView vote_tab_tv = view.findViewById(R.id.vote_tab_tv);
                    vote_tab_tv.setText("选项" + (vote_tab_ll.getChildCount() + 1));
                    EditText vote_tab_et = view.findViewById(R.id.vote_tab_et);
                    vote_tab_et.setText("");
                    ImageView vote_tab_iv = view.findViewById(R.id.vote_tab_iv);
                    vote_tab_iv.setVisibility(View.VISIBLE);
                    vote_tab_ll.addView(view);
                    vote_tab_iv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View clickview) {
                            vote_tab_ll.removeView(view);
                            for (int i = 0; i < vote_tab_ll.getChildCount(); i++) {
                                TextView vote_tab_tv = vote_tab_ll.getChildAt(i).findViewById(R.id.vote_tab_tv);
                                vote_tab_tv.setText("选项" + (i + 1));
                            }
                            add_vote.setVisibility(View.VISIBLE);
                        }
                    });
                }
                if (vote_tab_ll.getChildCount() == 4) {
                    add_vote.setVisibility(View.GONE);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
    }
}